import { Form } from 'ant-design-vue'
import { type Ref, onMounted, ref } from 'vue'

import { configApi, userApi } from '@/apis'
import { TableRefHook, useDialog } from '@/hooks'

import { useUserForm } from '.'

type Params = {
  getFormData: PromiseFunc
  tableRefHook: TableRefHook
}

const { useForm } = Form

export function useOperation({ getFormData, tableRefHook }: Params) {
  const selectedId = ref('')
  const initPassword = ref('')
  const showReset = ref(false)
  const resetLoading = ref(false)
  const resetFormModel = ref({
    password: ''
  } as IResetPasswordDTO)
  const resetFormRules = ref({
    password: [{ required: true, message: '密码不能为空' }]
  } as FormRules<IResetPasswordDTO>)
  const resetFormRef = useForm(resetFormModel, resetFormRules)

  const { showConfirm } = useDialog()
  const {
    show: showEdit,
    loading: editLoading,
    formModel: editFormModel,
    formRef: editFormRef
  } = useUserForm()

  function handleShowEdit(item: any) {
    const { userId, ...rest } = item
    selectedId.value = userId
    editFormModel.value = rest
    showEdit.value = true
    getFormData(userId).then(value => {
      editFormModel.value.postIds = value.postIds
      editFormModel.value.roleIds = value.roleIds
    })
  }

  function handleCloseEdit() {
    showEdit.value = false
  }

  function handleEdit() {
    editFormRef.validate().then(async () => {
      try {
        editLoading.value = true

        await userApi.updateUser(selectedId.value, editFormModel.value)

        editLoading.value = false
        handleCloseEdit()
        tableRefHook.refresh()
      } catch {
        editLoading.value = false
      }
    })
  }

  function handleDelete(id: string) {
    showConfirm({
      content: '确认删除？',
      onOk: async () => {
        await userApi.deleteUser({
          ids: [id]
        })

        tableRefHook.refresh()
      }
    })
  }

  function handleShowReset(id: string) {
    selectedId.value = id
    resetFormModel.value.password = initPassword.value
    showReset.value = true
  }

  function handleCloseReset() {
    showReset.value = false
  }

  function handleReset() {
    resetFormRef.validate().then(async () => {
      try {
        resetLoading.value = true

        await userApi.resetPassword(selectedId.value, resetFormModel.value)

        resetLoading.value = false
        handleCloseReset()
        tableRefHook.refresh()
      } catch {
        resetLoading.value = false
      }
    })
  }

  function handleToggleStatus(item: any) {
    showConfirm({
      content: `确定${item.status === '0' ? '停用' : '启用'}用户？`,
      onOk: async () => {
        await userApi.toggleUserStatus(item.userId)

        tableRefHook.refresh()
      },
      onCancel() {
        item.status = item.status === '0' ? '1' : '0'
      }
    })
  }

  async function getInitPassword() {
    const data = await configApi.getConfigValue('sys.user.initPassword')

    initPassword.value = data.configValue
  }

  onMounted(() => {
    getInitPassword()
  })

  return {
    showReset,
    resetLoading,
    resetFormModel,
    resetFormRef,
    showEdit,
    editLoading,
    editFormModel,
    editFormRef,
    handleShowEdit,
    handleCloseEdit,
    handleEdit,
    handleDelete,
    handleShowReset,
    handleCloseReset,
    handleReset,
    handleToggleStatus
  }
}
